package controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author 徒有琴
 */
@Controller
public class UploadController {

    @RequestMapping("upload1.html")
    public String upload(@RequestParam("file1") MultipartFile file, String str) {
        try {
            System.out.println(str);
            FileUtils.copyInputStreamToFile(file.getInputStream(),
                    new File("D:/upload/",
                            file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

    @RequestMapping("upload2.html")
    public String upload2(MultipartFile[] file) {
        try {
            for (int i = 0; i < file.length; i++) {
                MultipartFile multipartFile = file[i];
                String name = multipartFile.getOriginalFilename();
                String extName = name.substring(name.lastIndexOf("."));
                String time = System.currentTimeMillis() + "" + i + extName;
                System.out.println(name + "=" + time);
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),
                        new File("D:/upload/", time
                        ));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }
}
